from django.shortcuts import redirect
from django.core.urlresolvers import reverse
import re

class ShopMiddleware(object):
    def __init__(self,get_response):
        self.get_response=get_response
        print("ShopMiddleware")

    def __call__(self,request):
        print("__call__ShopMiddleware")
        # 定义网站后台不用登录也可访问的路由url
        urllist = ['/myadmin/login', '/myadmin/dologin', '/myadmin/logout','/myadmin/verify']
        # 当前请求路径
        curpath=request.path
        # 判断当前路径是否网站后台，并且不在urllist中
        if re.match("/myadmin",curpath) and curpath not in urllist:
            if 'adminuser' not in request.session:
                return redirect(reverse('myadmin_login'))
        response = self.get_response(request)
        return response


